/*
 * IMloginGui.java
 *
 * Created on March 26, 2008, 9:08 PM
 */

package imessenger;

import java.awt.Toolkit;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
/**
 *
 * @author  Shelley Jane C. Co
 */
public class IMloginGui extends JFrame {
    
    private IMClient imclient;
    private ProxyWindow proxyGui;
    
    /** Creates new form IMloginGui */
    public IMloginGui() {
        
        this.proxyGui = null;
        initComponents();
        KeyListener keyListener = new KeyAdapter()
        {
            public void keyPressed(KeyEvent e)
            {
                if(e.getKeyCode() == e.VK_ENTER)    //enter key
                {
                    doLogin();
                }
            }
        };
        this.unameTxtBx.addKeyListener(keyListener);
        this.pwordTxtBx.addKeyListener(keyListener);        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        loginPanel = new javax.swing.JPanel();
        unameTxtBx = new javax.swing.JTextField();
        pwordTxtBx = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        loginBtn = new javax.swing.JButton();
        standardMenuBar = new javax.swing.JMenuBar();
        menu = new javax.swing.JMenu();
        proxyMenuItem = new javax.swing.JMenuItem();

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("User Name:");

        jLabel2.setText("Password:");

        loginBtn.setText("Log In");
        loginBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout loginPanelLayout = new javax.swing.GroupLayout(loginPanel);
        loginPanel.setLayout(loginPanelLayout);
        loginPanelLayout.setHorizontalGroup(
            loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginPanelLayout.createSequentialGroup()
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(loginPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(unameTxtBx, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)))
                    .addGroup(loginPanelLayout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addComponent(loginBtn))
                    .addGroup(loginPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(pwordTxtBx, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)))
                .addContainerGap())
        );
        loginPanelLayout.setVerticalGroup(
            loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginPanelLayout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(unameTxtBx, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pwordTxtBx, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addComponent(loginBtn)
                .addGap(21, 21, 21))
        );

        menu.setText("Menu");

        proxyMenuItem.setText("Configure Proxy");
        proxyMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                proxyActionPerformed(evt);
            }
        });
        menu.add(proxyMenuItem);

        standardMenuBar.add(menu);

        setJMenuBar(standardMenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(loginPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(30, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(loginPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(198, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void loginBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginBtnActionPerformed
        // TODO add your handling code here:
        doLogin();
    }//GEN-LAST:event_loginBtnActionPerformed

    private void proxyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_proxyActionPerformed
        // TODO add your handling code here:
        this.proxyGui = new ProxyWindow();
        this.proxyGui.setVisible(true);        
    }//GEN-LAST:event_proxyActionPerformed
    
    private void doLogin()
    {
        System.out.println("Logging In.....");
        //System.out.println("Proxy? "+this.proxyGui.isProxy);
        
        if (unameTxtBx.getText().equals("") || pwordTxtBx.getPassword().length == 0)
        {
            System.out.println("User name/password is blank");            
        }
        else
        {
            imclient = new IMClient();
            if (this.proxyGui == null || this.proxyGui.isProxy == false)
            {
                imclient.login(unameTxtBx.getText(), pwordTxtBx.getPassword());
                this.setVisible(false);
                imclient.runIM();
            }
            else if (this.proxyGui.isProxy)
            {
                String host = this.proxyGui.getHost();
                String port = this.proxyGui.getPort();
                Properties systemProperties = System.getProperties();
                systemProperties.setProperty("http.proxySet", "true");
                systemProperties.setProperty("http.proxyHost",host);
                systemProperties.setProperty("http.proxyPort",port);
                
                imclient.login(unameTxtBx.getText(), pwordTxtBx.getPassword());
                this.setVisible(false);
                imclient.runIM();
            }
        }
    }
  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDialog jDialog1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton loginBtn;
    private javax.swing.JPanel loginPanel;
    private javax.swing.JMenu menu;
    private javax.swing.JMenuItem proxyMenuItem;
    private javax.swing.JPasswordField pwordTxtBx;
    private javax.swing.JMenuBar standardMenuBar;
    private javax.swing.JTextField unameTxtBx;
    // End of variables declaration//GEN-END:variables

    
}
